<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2019/6/11
 * Time: 16:53
 * Features: 基础命令行抽象类,只能继承
 */

namespace app\timeexecution\command;


use app\common\service\StringHelper;
use loger\Loger;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Config;

abstract class Base extends Command
{
    //配置信息
    protected $_config;
    //日志保存子目录
    protected $_logChildDir;

    /**
     * 构造函数,挂载配置
    */
    public function __construct($name = null)
    {
        $this->_config = Config::get('timeexecution.');
        $this->_logChildDir = trim($this->_config['log_child_dir'],DIRECTORY_SEPARATOR) .DIRECTORY_SEPARATOR. ltrim(StringHelper::humpToLine(strrchr(get_class($this),'\\')),'\_');
        parent::__construct($name);
    }

    /**
     * 写入日志
     * @param string $msg 内容
    */
    protected function saveLogs($msg){
        Loger::write($msg,$this->_logChildDir);
    }

    /**
     * 输出打印并写入日志
     * @param string $msg 内容
     * @param object $output think\console\Output 对象
     * @param bool   $isExit 是否打断点终止后续程序执行
    */
    protected function writeAndLogs($msg,Output $output,$isExit = true){
        $output->write($msg . "\r\n");
        $this->saveLogs($msg);
        if($isExit){
            exit();
        }
    }

    /**
     * 获取类名
    */
    protected function getClassName(){
        return ltrim(strrchr(get_class($this),'\\'),'\\');
    }

    /**
     * 配置命令
     */
    protected function configure()
    {
        parent::configure(); // TODO: Change the autogenerated stub
    }

    /**
     * 执行程序
     */
    protected function execute(Input $input, Output $output)
    {
        parent::execute($input, $output); // TODO: Change the autogenerated stub
    }

}